/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import { isCI } from '@ohos/hvigor-common';
import { basename, join, resolve } from 'path';

import { BuildArtifactConst, BuildDirConst } from '../../../src/const/build-directory-const';
import { CommonConst } from '../../../src/const/common-const';
import { InjectUtil } from '../../../src/utils/inject-util';

export class Probes {
  private _targetName: string;
  private _projectPath: string;
  private _productName: string;
  private _buildRoot: string;
  private _modulePath: string;

  constructor(projectPath: string, modulePath: string, targetName = 'default', productName = 'default', buildRoot = 'build') {
    this._targetName = targetName;
    this._projectPath = projectPath;
    this._productName = productName;
    this._buildRoot = buildRoot;
    this._modulePath = modulePath;
  }

  setTargetName(targetName: string): void {
    this._targetName = targetName;
  }

  setProductName(productName: string): void {
    this._productName = productName;
  }

  setModulePath(modulePath: string): void {
    this._modulePath = modulePath;
  }

  setProjectPath(projectPath: string): void {
    this._projectPath = projectPath;
  }

  setBuildRoot(buildRoot: string): void {
    this._buildRoot = buildRoot;
  }

  getProjectPath(): string {
    return this._projectPath;
  }

  getBuildRoot(): string {
    return this._buildRoot;
  }

  getModuleBuildPath(useDefault = false): string {
    return resolve(
      useDefault
        ? this._modulePath
        : InjectUtil.getBuildCacheParentDir(this._modulePath, join(basename(this._projectPath), this._modulePath.substring(this._projectPath.length))),
      this._buildRoot,
    );
  }

  getModuleProductPath(): string {
    return resolve(this.getModuleBuildPath(), this._productName);
  }

  getModuleBuildIntermediates(): string {
    return resolve(this.getModuleProductPath(), BuildDirConst.INTERMEDIATES);
  }

  getModuleBuildGenerated(): string {
    return resolve(this.getModuleProductPath(), BuildDirConst.GENERATED);
  }

  getModuleBuildOutputPath(): string {
    return resolve(this.getModuleProductPath(), BuildDirConst.BUILD_OUTPUTS, this._targetName);
  }

  getModuleBuildOutputRichPath(): string {
    return resolve(this.getModuleProductPath(), BuildDirConst.BUILD_OUTPUTS, this._targetName, 'rich');
  }

  getModuleBuildOutputLitePath(): string {
    return resolve(this.getModuleProductPath(), BuildDirConst.BUILD_OUTPUTS, this._targetName, 'lite');
  }

  getIntermediatesMergeRes(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_RES, this._targetName);
  }

  getBuildJavaDir(): string {
    return resolve(this.getModuleBuildIntermediates(), 'java', this._targetName);
  }

  getBuildDexDir(): string {
    return resolve(this.getModuleBuildIntermediates(), 'dex', this._targetName);
  }

  getIntermediatesMergeFile(): string {
    return resolve(this.getIntermediatesMergeRes(), BuildArtifactConst.MERGE_RES_FILE_INDEX);
  }

  getIntermediatesMergeProfile(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_PROFILE, this._targetName, CommonConst.MODULE_JSON);
  }

  getIntermediatesMergeProfileDir(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_PROFILE, this._targetName);
  }

  getGenerateSourceR(): string {
    return resolve(this.getModuleBuildGenerated(), BuildDirConst.R, this._targetName);
  }

  getIntermediatesProcessProfile(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_PROCESS_PROFILE, this._targetName, CommonConst.MODULE_JSON);
  }

  getIntermediatesProcessLegacyProfile(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_PROCESS_PROFILE, this._targetName, CommonConst.CONFIG_JSON);
  }

  getIntermediatesRes(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_RES, this._targetName);
  }

  getIntermediatesManifest(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LEGACY_MANIFEST, this._targetName);
  }

  getIntermediatesOriginalRes(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_ORIGINAL_RES, this._targetName);
  }

  /**
   * restool编译结果的产物profile的路径
   * 其中resources,base,profile是固定的，是restool中的定义
   *
   * @return ../intermediates/res/{targetName}/res/base/profile
   */
  getIntermediatesResProfilePath(): string {
    return resolve(this.getIntermediatesRes(), BuildDirConst.RESTOOL_BUILD_RESOURCES, BuildDirConst.BASE, BuildDirConst.PROFILE);
  }

  getIntermediatesAssetsPath(compileMode = 'jsbundle'): string {
    return resolve(this.getModuleBuildIntermediates(), `${BuildDirConst.JS_BUILD_ASSETS}_${compileMode}`, this._targetName);
  }

  getIntermediatesLoaderPath(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LOADER, this._targetName);
  }

  getIntermediatesLiteSourcePath(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LITE_SOURCES, this._targetName);
  }

  getIntermediatesLoaderOutPath(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LOADER_OUT, this._targetName);
  }

  getModuleBuildCachePath(): string {
    return resolve(this.getModuleProductPath(), BuildDirConst.BUILD_CACHE, this._targetName);
  }

  getIntermediatesMergeLegacyProfile(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_PROFILE, this._targetName, CommonConst.CONFIG_JSON);
  }

  getIntermediatesLegacyManifestJson(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LEGACY_MANIFEST, this._targetName);
  }

  getIntermediatesPackInfoDir(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LEGACY_MANIFEST, this._targetName);
  }

  getIntermediatesProcessLibs(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.LIBS, this._targetName);
  }

  getIntermediatesSysCap(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.SYSCAP, this._targetName);
  }

  getIntermediatesOutputMetadata(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.HAP_METADATA, this._targetName);
  }

  /**
   * 根据是否是CI环境不同的处理，是CI环境则返回用户自定义路径，不是则返回默认路径
   */
  getCppOutputDir(): string {
    return resolve(
      isCI()
        ? InjectUtil.getBuildCacheParentDir(this._modulePath, join(basename(this._projectPath), this._modulePath.substring(this._projectPath.length)))
        : this._modulePath,
      BuildDirConst.CPP_OUTPUT,
    );
  }

  getModuleShellDir(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.SHELL, this._targetName);
  }

  getShellResourceDir(): string {
    return resolve(this.getModuleShellDir(), BuildDirConst.SRC, BuildDirConst.MAIN, BuildDirConst.INTERMEDIATES_RES);
  }

  getShellBuildDir(): string {
    return resolve(this.getModuleShellDir(), BuildDirConst.BUILD_ROOT);
  }

  getIntermediatesCppOutPut(): string {
    return resolve(this.getModuleBuildIntermediates(), BuildDirConst.CMAKE, this._targetName, BuildDirConst.OBJ);
  }

  getProjectBuildPath(): string {
    return resolve(InjectUtil.getBuildCacheParentDir(this._projectPath, basename(this._projectPath), false), BuildDirConst.BUILD_ROOT);
  }

  getProjectOutputPath(): string {
    return resolve(this.getProjectBuildPath(), BuildDirConst.BUILD_OUTPUTS, this._productName);
  }

  getShellSrcMain(): string {
    return resolve(this.getModuleShellDir(), BuildDirConst.SRC, BuildDirConst.MAIN);
  }
}
